<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售单号" prop="orderSell">
        <el-input
          v-model="queryParams.orderSell"
          placeholder="请输入销售单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交付日期" prop="orderDay">
        <el-date-picker clearable
          v-model="queryParams.orderDay"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择交付日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>




    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="生产订单编号" align="center" prop="orderId" />
      <el-table-column label="销售单号" align="center" prop="orderSell" />
      <el-table-column label="交付日期" align="center" prop="orderDay" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDay, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="orderLevel" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderLevel===1">普通</el-tag>
          <el-tag v-if="scope.row.orderLevel===2">紧急</el-tag>
          <el-tag v-if="scope.row.orderLevel===3">非常紧急</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="计划产量" align="center" prop="plannedQuantity" />
      <el-table-column label="生产状态" align="center" prop="productionStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.productionStatus===1">待计划</el-tag>
          <el-tag v-if="scope.row.productionStatus===2">已计划</el-tag>
          <el-tag v-if="scope.row.productionStatus===3">已完成</el-tag>
          <el-tag v-if="scope.row.productionStatus===4">已作废</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="已生产数量" align="center" prop="producedQuantity" />
      <el-table-column label="客户" align="center" prop="customerName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="creationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="handlePlan(scope.row)"
            v-hasPermi="['order:order:plan']"
          >创建生产计划</el-button>

           <el-button
            size="mini"
            type="text" icon="el-icon-s-opportunity"
            @click="handlePlangive(scope.row)"
            v-hasPermi="['order:order:plan']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-opportunity"
            @click="handleDetail(scope.row)"
            v-hasPermi="['order:order:detail']"
          >详情</el-button>

          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handlegive( scope.row)"
              v-hasPermi="['order:order:give']"
            >新增</el-button>
          </el-col>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSell: null,
        orderDay: null,
        orderLevel: null,
        productCode: null,
        productName: null,
        unit: null,
        specification: null,
        plannedQuantity: null,
        productionStatus: null,
        producedQuantity: null,
        customerName: null,
        creator: null,
        creationTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //新增生产订单
    handlegive(row) {
        this.$router.push({
          path: '/order/give',
          query: { orderId: row.orderId } // 将当前行的id传递到新增生产订单页面
          })
    },

//生产订单详情
    handleDetail(row) {
        this.$router.push({
          path: '/order/plan',
          query: { orderId: row.orderId } // 将当前行的id传递到详情页面
          })
    },

    //跳转新增生产订单
    handlePlangive(row) {
        this.$router.push({
          path: '/order/give',
          query: { orderId: row.orderId } // 将当前行的id传递到生产计划页面
          })
    },
    //跳转到创建生产计划
    handlePlan(row) {
        this.$router.push({
          path: '/order/mation',
          query: { orderId: row.orderId } // 将当前行的id传递到生产计划页面
          })

    },
    /** 查询生产订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderSell: null,
        orderDay: null,
        orderLevel: null,
        productCode: null,
        productName: null,
        unit: null,
        specification: null,
        plannedQuantity: null,
        productionStatus: null,
        producedQuantity: null,
        customerName: null,
        remark: null,
        creator: null,
        creationTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除生产订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
